package com.ty.jpa;

import com.ty.jpa.entity.Role;
import com.ty.jpa.entity.User;
import com.ty.jpa.repository.RoleRepository;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * RoleRepositoryTester
 *
 * @aurhor Administrator  whs
 * @since 2024/8/31
 */
@SpringBootTest
public class RoleRepositoryTester {

    @Resource
    private RoleRepository roleRepository;

    @Test
    public void testGet() { // 双向一对多关联查询

        Role role = roleRepository.findById(1L).get();
        System.out.println("roleName : "+role.getRoleName());
        System.out.println("getRoleId : "+role.getRoleId());

        System.out.println("user.size : "+role.getUsers().size());
    }

    @Test
    public void testAdd(){ //测试级联新增
        Role role = new Role(1,"演示级联新增角色和用户","小猫老弟");
        User user1 = new User("测试小猫老弟","666666",role,1L);
        User user2 = new User("测试涂山炜","333333",role,1L);
        role.getUsers().add(user1);
        role.getUsers().add(user2);
        roleRepository.save(role); //新增角色的同时新增关联的用户
    }

    @Test
    public void testDelete(){  //  测试级联删除
        // 先使用getOne 方法先获取到role的引用，然后调用delete方法删除
        Role role = roleRepository.findById(35L).get();
        roleRepository.delete(role);
    }


}
